package org.example.code.test2024.ten_mouth;

import java.util.Arrays;

public class test1014 {
    public boolean canPartition(int[] nums) {
        int sum = 0;
        for(int num:nums){
            sum=sum+num;
        }
        Arrays.sort(nums);
        if(sum%2!=0){
            return false;
        }
        int result = sum/2;
        boolean[] dp = new boolean[result + 1];
        dp[0] = true;
        for (int i = 1; i <= result; i++) {
            for (int j = result; j >= nums[i - 1]; j--) {
                dp[j] |= dp[j - nums[i - 1]];
            }
        }
        return dp[result];
    }

    public int numDistinct(String s, String t) {
        int m = s.length();
        int n = t.length();

        int[][] dp = new int[m+1][n+1];

        for(int i=0;i<=m;i++){
            dp[i][0]=1;
        }
        for(int i=1;i<=m;i++){
            for(int j=1;j<=n;j++){
                if(s.charAt(i-1)==t.charAt(j-1)){
                    dp[i][j]=dp[i-1][j]+dp[i-1][j-1];
                }else{
                    dp[i][j]=dp[i-1][j];
                }
            }
        }
        return dp[m][n];
    }

    public boolean articleMatch(String article, String input) {
        int m = input.length();
        int n = article.length();
        boolean[][] dp = new boolean[m + 1][n + 1];
        dp[0][0] = true;
        for (int i = 1; i <= m; i++) {
            if (input.charAt(i - 1) == '*' && dp[i - 2][0]) {
                dp[i][0] = true;
            }
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                char in = input.charAt(i - 1);
                char art = article.charAt(j - 1);
                if (in == art || in == '.') {
                    dp[i][j] = dp[i - 1][j - 1];
                }
                else if (in == '*') {
                    dp[i][j] = dp[i - 2][j];
                    if (input.charAt(i - 2) == art || input.charAt(i - 2) == '.') {
                        dp[i][j] = dp[i][j] || dp[i][j - 1];
                    }
                }
            }
        }
        return dp[m][n];
    }

}
